/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.dragonways.client;

import org.meanzoft.m3d.scene.SceneNode;
import org.meanzoft.minecraft_clone.util.VertexBuffer;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Meanz
 */
public class Human extends SceneNode {

    private int displayList;
    private VertexBuffer vertices;
    private VertexBuffer texCoords;

    public Human() {
        yaw = 0f;
        pitch = 0f;
        vertices = new VertexBuffer();
        texCoords = new VertexBuffer();
        addBox(0.2f, 1f, 0f, 0.2f, 0.2f, new int[]{0, 0, 0, 0, 0, 0}); //Right Arm
        addBox(0.2f, 1f, 1f, 0.2f, 0.2f, new int[]{0, 0, 0, 0, 0, 0}); //Left Arm
        addBox(0.8f, 1f, 0.2f, 0.2f, 0f, new int[]{1, 1, 1, 1, 1, 1}); //Body
        addBox(0.4f, 0.5f, 0.38f, 1.2f, 0.225f, new int[]{2, 2, 2, 2, 2, 2}); //Head
        addBox(0.2f, 1f, 0.2f, -0.5f, 0.2f, new int[]{3, 3, 3, 3, 3, 3}); //Right Foot
        addBox(0.2f, 1f, 0.8f, -0.5f, 0.2f, new int[]{3, 3, 3, 3, 3, 3}); //Left Arm
    }

    public void addBox(float width, float height, float offX, float offY, float offZ, int[] textures) {
        vertices.expand(6 * 4);
        texCoords.expand(6 * 4);
        //normals.expand(numFaces);

        float off = (float) ((float) 1 / (float) 16);
        float[] texX = new float[6];
        float[] texY = new float[6];

        for (int i = 0; i < 6; i++) {
            texX[i] = textures[i] * off;
            texY[i] = off * (textures[i] / 16);
        }
        texCoords.putVertex(0f, texX[0], texY[0] + off);
        vertices.putVertex(offX + width, offY, offZ);
        texCoords.putVertex(0f, texX[0] + off, texY[0] + off);
        vertices.putVertex(offX, offY, offZ);
        texCoords.putVertex(0f, texX[0] + off, texY[0]);
        vertices.putVertex(offX, height + offY, offZ);
        texCoords.putVertex(0f, texX[0], texY[0]);
        vertices.putVertex(width + offX, height + offY, offZ);
        texCoords.putVertex(0f, texX[1], texY[1] + off);
        vertices.putVertex(offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[1] + off, texY[1] + off);
        vertices.putVertex(width + offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[1] + off, texY[1]);
        vertices.putVertex(width + offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[1], texY[1]);
        vertices.putVertex(offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[2], texY[2] + off);
        vertices.putVertex(width + offX, height + offY, offZ);
        texCoords.putVertex(0f, texX[2] + off, texY[2] + off);
        vertices.putVertex(offX, height + offY, offZ);
        texCoords.putVertex(0f, texX[2] + off, texY[2]);
        vertices.putVertex(offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[2], texY[2]);
        vertices.putVertex(width + offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[3], texY[3] + off);
        vertices.putVertex(width + offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[3] + off, texY[3] + off);
        vertices.putVertex(offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[3] + off, texY[3]);
        vertices.putVertex(offX, offY, offZ);
        texCoords.putVertex(0f, texX[3], texY[3]);
        vertices.putVertex(width + offX, offY, offZ);
        texCoords.putVertex(0f, texX[4], texY[4] + off);
        vertices.putVertex(width + offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[4] + off, texY[4] + off);
        vertices.putVertex(width + offX, offY, offZ);
        texCoords.putVertex(0f, texX[4] + off, texY[4]);
        vertices.putVertex(width + offX, height + offY, offZ);
        texCoords.putVertex(0f, texX[4], texY[4]);
        vertices.putVertex(width + offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[5], texY[5] + off);
        vertices.putVertex(offX, offY, offZ);
        texCoords.putVertex(0f, texX[5] + off, texY[5] + off);
        vertices.putVertex(offX, offY, width + offZ);
        texCoords.putVertex(0f, texX[5] + off, texY[5]);
        vertices.putVertex(offX, height + offY, width + offZ);
        texCoords.putVertex(0f, texX[5], texY[5]);
        vertices.putVertex(offX, height + offY, offZ);
    }
    
    public void update() {
        
    }
    
    public void draw() {
        glBegin(GL_QUADS);
        {
            for (int i = 0; i < vertices.size(); i++) {
                float[] vertex = vertices.getVertex(i).toFloatArray();
                float[] texCoord = texCoords.getVertex(i).toFloatArray();

                glTexCoord2f(texCoord[1], texCoord[2]);
                glVertex3f(vertex[0], vertex[1], vertex[2]);
            }
        }
        glEnd();
    }
}
